<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        #popup .layui-form {
            width: 80%;
            text-align: left;
        }
        .laytable-cell-customization {
            text-align: center;
            height: auto !important;
            white-space: normal;
            overflow: visible;
            text-overflow: inherit;
            word-break: break-all;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item custom-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">服务商编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="providerId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">平台ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">真实姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="realName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">关联手机</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">服务类型</label>
                            <div class="layui-input-inline">
                                <select name="storeType">
                                    <option value="">不限</option>
                                    <option value="线上服务商">线上服务商</option>
                                    <option value="线上店铺">线上店铺</option>
                                    <option value="主播达人">主播达人</option>
                                    <option value="线下零售">线下零售</option>
                                    <option value="线下商贸">线下商贸</option>
                                    <option value="线下网点">线下网点</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开团</label>
                            <div class="layui-input-inline">
                                <select name="isInitiateGroup">
                                    <option value="">不限</option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">自购</label>
                            <div class="layui-input-inline">
                                <select name="firstOrderStatus">
                                    <option value="">不限</option>
                                    <option value="未完成">是</option>
                                    <option value="已完成">否</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">出单</label>
                            <div class="layui-input-inline">
                                <select name="issue">
                                    <option value="">不限</option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">推荐人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="referrer" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">创建时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="dateRange" id="dateRange" class="layui-input"
                                       placeholder="请选择时间区间">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                        </button>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" id="deleteBtn"
                        style="display: none" lay-event="delete">删除
                </button>
                <button class="layui-btn layui-btn-warm layui-btn-sm data-add-btn" lay-event="exportBtn"> 导出</button>
                <button class="layui-btn layui-btn-sm layui-btn-disabled data-delete-btn provide-edit"
                        style="display: none" id="editBtn"
                        lay-event="edit">
                    修改
                </button>
            </div>
        </script>

        <div id="popup" style="display:none">
            <div class="layui-form  layuimini-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">首单</label>
                    <div class="layui-input-block">
                        <select name="firstOrderStatus" id="firstOrderStatus">
                            <option value="">请选择</option>
                            <option value="未完成">未完成</option>
                            <option value="已完成">已完成</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">推荐人</label>
                    <div class="layui-input-block">
                        <input type="text" name="referrer" value="" id="referrer" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="edit">
                修改
            </button>
        </script>
        <script type="text/html" id="imgTemp">
            <img src="{{d.storeAvatar}}" style="width: 100%;height: 45px;"/>
        </script>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/common/permissions.js" charset="utf-8"></script>
<script>
    let myClos;
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#dateRange', //指定元素
            max: 0, // 限制最大值为当前日期
            type: 'date',
            range: true, //开启范围选择
        });

        table.render({
            elem: '#currentTableId',
            url: '/provider/list',
            method: 'POST',
            contentType: 'application/json',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", fixed: 'left',style: 'height:60px;'},
                {
                    field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                        return d.LAY_INDEX; //返回序号
                    }
                },
                {
                    field: 'storeAvatar', title: '头像', align: 'center', minWidth: 80, templet: '#imgTemp',
                    style: 'height:60px;'
                },
                {field: 'createTime', title: '创建时间', sort: true, align: 'center', minWidth: 120},
                {field: 'providerId', title: '服务商编号', sort: true, align: 'center', minWidth: 120},
                {field: 'storeType', title: '服务类型', align: 'center', minWidth: 120},
                {field: 'storeName', title: '服务商名称', align: 'center', minWidth: 120},
                {field: 'area', title: '所在地区', align: 'center', minWidth: 120},
                {field: 'street', title: '所在街道', align: 'center', minWidth: 120},
                {field: 'userId', title: '平台ID', align: 'center', minWidth: 120},
                {field: 'nickName', title: '平台昵称', align: 'center', minWidth: 120},
                {field: 'phone', title: '关联手机', align: 'center', minWidth: 120},
                {field: 'realName', title: '真实姓名', align: 'center', minWidth: 100},
                {field: 'sex', title: '性别', sort: true, align: 'center', minWidth: 80},
                {field: 'age', title: '年龄层', sort: true, align: 'center', minWidth: 80},
                {
                    field: 'isInitiateGroup',
                    title: '开团',
                    sort: true,
                    align: 'center',
                    minWidth: 100,
                    templet: function (d) {
                        if (d.isInitiateGroup === '1') {
                            let userId = "'" + d.userId + "'";
                            return '<span style="color:#4fc44f;cursor:pointer;" onclick="toPublishGroupData(' + userId + ')">查看团购</span>'
                        } else {
                            return '<span style="color:#ff0000;">否</span>'
                        }
                    }
                },
                {
                    field: 'firstOrderStatus',
                    title: '自购',
                    sort: true,
                    align: 'center',
                    minWidth: 80,
                    templet: function (d) {
                        if (d.firstOrderStatus === '已完成') {
                            return '<span style="color:#4fc44f;">是</span>'
                        } else {
                            return '<span style="color:#ff0000;">否</span>'
                        }
                    }
                },
                {
                    field: 'issue',
                    title: '出单',
                    sort: true,
                    align: 'center',
                    minWidth: 80,
                    templet: function (d) {
                        if (d.issue === '1') {
                            return '<span style="color:#4fc44f;">是</span>'
                        } else {
                            return '<span style="color:#ff0000;">否</span>'
                        }
                    }
                },
                {
                    field: 'bindOfficialAccount',
                    title: '公众号',
                    sort: true,
                    align: 'center',
                    minWidth: 100,
                    templet: function (d) {
                        if (d.bindOfficialAccount === '是') {
                            return '<span style="color:#4fc44f;">是</span>'
                        } else {
                            return '<span style="color:#ff0000;">否</span>'
                        }
                    }
                },
                {field: 'referrer', title: '推荐人', align: 'center', minWidth: 120},
            ]],
            limits: [50, 100, 150, 200, 250, 300],
            limit: 50,
            page: true,
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            response: { //定义后端 json 格式，详细参见官方文档
                statusName: 'code', //状态字段名称
                statusCode: '200', //状态字段成功值
                msgName: 'msg', //消息字段
                countName: 'total', //总数字段
                dataName: 'rows' //数据字段
            },
            done: function () {
                const btnRoles = {
                    "editBtn": 'admin',
                    "deleteBtn": 'admin',
                }
                buttonPermissions(btnRoles)
                // 给指定的列加上css样式
                let colsList = this.cols[0];
                myClos = colsList;
                for (let cols of colsList) {
                    if (cols.field === 'storeAvatar' || cols.title === '操作') {
                        // 添加样式
                        let classStr = ".laytable-cell-1-" + cols.key;
                        $(classStr).addClass('laytable-cell-customization')//
                    }
                }
            }
        });

        // 监听表格排序事件
        table.on('sort(currentTableFilter)', function (obj) {
            for (let cols of myClos) {
                if (cols.field === 'storeAvatar' || cols.title === '操作') {
                    // 添加样式
                    let classStr = ".laytable-cell-1-" + cols.key;
                    $(classStr).addClass('laytable-cell-customization')//
                }
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let dataRangeList = data.field.dateRange.split(` - `)
            let dataRange = {}
            if (dataRangeList.length === 2) {
                dataRange = {
                    'startDate': dataRangeList[0],
                    'endDate': dataRangeList[1]
                }
            }
            data.field.dateRange = JSON.stringify(dataRange);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: data.field
            }, 'data');

            return false;
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'exportBtn') {  // 监听导出操作
                let checkStatus = table.checkStatus('currentTableId');
                let data = {
                    ids: [],
                    batchType: 0,
                    provider: obj.config?.where,
                }
                if (checkStatus.isAll) {
                    data.batchType = 1;
                } else {
                    let dataList = checkStatus.data;
                    if (dataList.length > 0) {
                        let ids = [];
                        dataList.forEach(function (item) {
                            ids.push(item.id)
                        });
                        data.ids = ids;
                    } else {
                        data.batchType = 1;
                    }

                }
                downloadExcel('/provider/list/export', data, '区域服务商.xlsx', 'POST')
            }
            if (obj.event === 'edit') {  // 监听添加操作
                const index = layer.open({
                    title: '修改信息',
                    type: 1,
                    btn: ['确定', '取消'],
                    content: $('#popup'),
                    area: ['40%', '270px'],
                    success: function (layero, index) {
                        let checkStatus = table.checkStatus('currentTableId')
                            , data = checkStatus.data;
                        if (data.length === 1) {
                            $("#firstOrderStatus").each(function () {
                                $(this).children("option").each(function () {
                                    // 判断需要对那个选项进行回显
                                    if (this.innerText === data[0].firstOrderStatus) {
                                        // 进行回显
                                        $(this).attr("selected", "selected");
                                    }
                                });
                                form.render('select');
                            })
                            $("#referrer").val(data[0].referrer)
                        } else {
                            layer.msg("数据异常");
                            layer.close(index);
                        }
                    },
                    yes: function (index, layero) {
                        // 组装参数
                        const requestData = {
                            firstOrderStatus: $("#firstOrderStatus").val(),
                            referrer: $("#referrer").val()
                        }
                        let checkStatus = table.checkStatus('currentTableId')
                            , data = checkStatus.data;
                        $.ajax({
                            type: "post",
                            url: '/provider/edit/' + data[0].id,
                            data: JSON.stringify(requestData),
                            dataType: 'json',
                            contentType: 'application/json',
                            success: function (req) {
                                if (req.code === '200') {
                                    $("#firstOrderStatus").val('')
                                    $("#referrer").val('')
                                    layer.msg("操作成功");
                                    layer.close(index);
                                    reloadTable();
                                } else {
                                    layer.msg("操作失败，请重试");
                                }
                            },
                            error: function (e) {
                                layer.msg("操作失败，请重试");
                            }
                        })
                    },
                    btn2: function (index, layero) {
                        // 关闭弹窗
                    },
                    end: function () {
                    }
                });
            }
            if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length > 0) {
                    layer.confirm('确定删除吗', function (index) {
                        let idList = []
                        for (const i in data) {
                            idList.push(data[i].id);
                        }
                        $.ajax({
                            type: "post",
                            url: '/provider/delete',
                            data: JSON.stringify(idList),
                            dataType: 'json',
                            contentType: 'application/json',
                            success: function (req) {
                                if (req.code == 200) {
                                    layer.msg("已删除");
                                    reloadTable();
                                } else {
                                    layer.msg("删除失败");
                                }
                            },
                            error: function (e) {
                                layer.msg("删除失败");
                            }
                        })
                        layer.close(index);
                    });
                } else {
                    layer.alert('请至少选中一行数据');
                }
            }
            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            // console.log(obj)
            let checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            if (data.length === 1) {
                // 修改按钮启用
                $(".provide-edit").removeClass('layui-btn-disabled')
                $(".provide-edit").addClass('layui-btn-normal')
            } else {
                $(".provide-edit").remove('layui-btn-normal')
                $(".provide-edit").addClass('layui-btn-disabled')
            }
        });

        // 监听表格双击事件
        table.on('rowDouble(currentTableFilter)', function (obj) {
            const rowData = obj?.data;
            if (rowData !== void 0 && rowData !== null) {
                layer.open({
                    title: '服务商详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: './edit.html?id=' + rowData.id,
                });
            } else {
                layer.msg("数据异常，请重新尝试");
            }
        });
    });

    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/provider/list',
            });
        })
    }

    function toPublishGroupData(userId) {
        if (isNotNull(userId)) {
            layer.open({
                title: '服务商详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../marketing/groupbuy-record.html?userId=' + userId,
            });
        } else {
            layer.msg("数据异常，请重新尝试");
        }
    }
</script>

</body>
</html>
